<template>
  <el-dialog
    :model-value="showQuestionTypeVisible"
    :title="props.productType === '0'?'学员班型':'会员卡权益'"
    width="50%"
    :before-close="resetForm"
    :close-on-click-modal="false"
  >
    <el-form
      :model="searchForm"
      ref="searchFormRef"
      label-width="82px"
      style="padding-bottom: 1.5rem"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="班型信息" prop="storeName" v-if="props.productType === '0'">
            <el-input v-model="searchForm.storeName" clearable @blur="getData" />
          </el-form-item>
          <el-form-item label="会员卡信息" prop="queryCondition" v-else>
            <el-input v-model="searchForm.queryCondition" clearable @blur="getData" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <art-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      :currentPage="pageNum"
      :pageSize="pageSize"
      :total="pageTotal"
    >
      <template #default>
        <el-table-column type="selection" reserve-selection width="55" />
        <el-table-column label="ID" prop="id" width="150" />
        <el-table-column label="排序" prop="sort" />
        <el-table-column
          :label="props.productType === '0' ? '班型信息' : '会员卡信息'"
          prop="typename"
        >
          <template #default="scope">
            <div class="title_box">
              <div class="title_box_left" v-if="scope.row.image">
                <img class="tableimg" :src="scope.row.image" />
              </div>
              <div class="title_box_right">
                <div class="bankcont_title" v-if="scope.row.name">
                  <div class="bankcont_title_text">{{ scope.row.name }}</div>
                </div>
                <div style="clear: both"></div>
                <div class="cartcont" v-if="scope.row.tags">
                  <el-button v-for="tag in scope.row.tags.split(',')" :key="tag" round size="small">
                    {{ tag }}
                  </el-button>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="售价（元）" prop="price" v-if="props.productType === '0'">
          <template #default="scope"> {{ scope.row.price }} </template>
        </el-table-column>
        <el-table-column label="售价/天数" prop="price" v-if="props.productType === '1'">
          <template #default="scope">
            {{ `${scope.row.price}元/${scope.row.validity}天` }}
          </template>
        </el-table-column>
      </template>
    </art-table>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确认</el-button>
        <el-button @click="resetForm">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
  import { ref, computed, reactive } from 'vue'
  import { ElMessage, UploadProps } from 'element-plus'
  import { useUserStore } from '@/store/modules/user'
  import { StudentClassTypeLis } from '@/api/driverSchoolApi'
  import { addProduct } from '@/api/marketing'
  import { vipEquity, vipType } from '@/api/userStudentsApi'

  const props = defineProps({
    showQuestionTypeVisible: {
      type: Boolean
    },
    productType: {
      type: String
    }
  })

  console.log(props.productType, 'productType')

  const emit = defineEmits(['closeDialog'])

  const resetForm = () => {
    emit('closeDialog')
  }
  const searchForm = reactive({
    storeName: '',
    queryCondition: ''
  })

  const selectIds = ref<string[]>([])
  function handleSelectionChange(data: any[]) {
    console.log(data, 'data==')
    selectIds.value = data.map((item) => item.id)
  }

  const filterEmptyValues = (obj: any) => {
    const result: any = {}
    for (const key in obj) {
      if (obj[key] !== '' && obj[key] !== null && obj[key] !== undefined) {
        result[key] = obj[key]
      }
    }
    return result
  }

  const tableData = ref([])
  const tableLoading = ref(false)
  const pageNum = ref(1)
  const pageSize = ref(10)
  const pageTotal = ref(0)
  const editLoading = ref(false)
  const getData = async () => {
    try {
      tableLoading.value = true
      const params = filterEmptyValues(searchForm)
      if (props.productType === '0') {
        const { code, data } = await StudentClassTypeLis({
          pageNum: pageNum.value,
          pageSize: pageSize.value,
          ...params
        })
        if (code === '00000' && data) {
          tableData.value = data.list
          pageTotal.value = data.total
        }
      } else {
        const { code, data } = await vipEquity({
          pageNum: pageNum.value,
          pageSize: pageSize.value,
          queryIsVip: 1,
          ...params
        })
        if (code === '00000' && data) {
          tableData.value = data.list
          pageTotal.value = data.total
        }
      }
    } catch (e) {
    } finally {
      tableLoading.value = false
    }
  }

  const handleSubmit = async () => {
    try {
      editLoading.value = true
      const params = {
        productIds: selectIds.value.join(','),
        productType: props.productType === '0' ? 1 : 0,
        brokerageRule: 0
      }
      const res = await addProduct(params)
      if (res.code === '00000') {
        ElMessage.success('新增成功')
        emit('closeDialog', true)
      }
    } catch {
    } finally {
      editLoading.value = false
    }
  }

  interface vipItem {
    value: string
    label: string
  }
  const vipData = ref<vipItem[]>([])
  const getVipList = async () => {
    const res = await vipType()
    if (res.code === '00000') {
      vipData.value = res.data
    }
  }

  onMounted(() => {
    getData()
    if (props.productType === '1') {
      getVipList()
    }
  })
</script>
<style scope lang="scss">
  .title_box_left {
    float: left;
    width: 4.5rem;
    height: 4.5rem;
    border-radius: 0.5rem;
    overflow: hidden;
    margin-right: 1rem;

    .tableimg {
      width: 100%;
      height: 100%;
    }
  }

  .title_box_right {
    float: right;
    float: left;
  }

  .bankcont_title_text {
    height: 2rem;
    margin-top: 0.3rem;
  }
</style>
